#include "stdio.h"
#include "SDL/SDL.h"

int main(int argc, char *argv[])
{
    int x ,y;
    SDL_Surface *screen;	//This pointer will reference the backbuffer
    SDL_Surface *image,*azerty,*b1,*b2,*b3,*b4;	//This pointer will reference our bitmap sprite
    //SDL_Surface *temp2,*temp,*temp1;//This pointer will temporarily reference our bitmap 	sprite
    SDL_Rect src1,src2,src,dest1,dest,dest2,sb1,sb2,sb3,sb4,db1,db2,db3,db4;//These rectangles will describe the source and 	destination regions of our blit
    SDL_Rect dstrect;
    SDL_Surface *loadImage(char *name);
    int SDL_OpenAudio(SDL_AudioSpec *desired, SDL_AudioSpec *obtained);
    int compteur=1,compteur2=1,i=0;

    int done =0;
    SDL_Event event ;
    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);

    //We must first initialize the SDL video component, and check for success
    if (SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
        return 1;
    }
    //Set the video mode to fullscreen 640x480 with 16bit colour and double-	buffering
    screen = SDL_SetVideoMode(960 , 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    if (screen == NULL)
    {
        printf("Unable to set video mode: %s\n", SDL_GetError());
        return 1;
    }
    //Load the bitmap into a temporary surface, and check for success
    image = SDL_LoadBMP("img/image.bmp");
    if(image == NULL)
    {
        printf("erreur loading image.bmp");
        return(2);
    }
    azerty = SDL_LoadBMP("img/azerty.bmp");
    if(azerty == NULL)
    {
        printf("erreur loading azerty.bmp");
        return(3);
    }

    b1 = SDL_LoadBMP("img/b1.bmp");
    if(b1 == NULL)
    {
        printf("erreur loading b1.bmp");
        return(3);
    }
    b2 = SDL_LoadBMP("img/b2.bmp");
    if(b2 == NULL)
    {
        printf("erreur loading b2.bmp");
        return(3);
    }
    b3 = SDL_LoadBMP("img/b3.bmp");
    if(b3 == NULL)
    {
        printf("erreur loading b3.bmp");
        return(3);
    }
    //Construct the source rectangle for our blit
    src2.x = 0;
    src2.y = 0;
    src2.w = image->w;
    src2.h = image->h;
    src.x = 0;
    src.y = 0;
    src.w = image->w;	//Use image->w to display the entire width of the image
    src.h = image->h;	//Use image->h to display the entire height of the image
    //Construct the destination rectangle for our blit
    src1.x = 0;
    src1.y = 0;
    src1.w = azerty->w;	//Use image->w to display the entire width of the image
    src1.h = azerty->h;
    dest.x = 0;//Display the image at the (X,Y) coordinates (100,100)
    dest.y = 0;
    dest.w = image->w;
    dest.h = image->h;
    dest1.x = 0;//Display the image at the (X,Y) coordinates (100,100)
    dest1.y = 510;
    dest1.w =azerty->w;
    dest1.h = azerty->h;
    sb1.x=0;
    sb1.y=0;
    sb2.x=0;
    sb2.y=0;
    sb3.x=0;
    sb3.y=0;
    //SDL_EnableKeyRepeat(10,10);
    SDL_SetColorKey(azerty,SDL_SRCCOLORKEY,SDL_MapRGB(azerty->format,255,255,255));
    //SDL_EnableKeyRepeat(200,10);
    /************************ Menu LOOP ***********************************/
    while(!done)
    {
        /********************NEVER LOAD IMAGES IN THE GAME LOOP !!!!!!!!!!!!!!!!!!!!!!!*******************/
        /***************************(1)Logic Game: Collision + moving entity + physics + events listener *******************************/

        while (SDL_PollEvent(&event))
        {

            switch(event.type)
            {
            case SDL_QUIT:
                done=1;
            case SDL_KEYDOWN:
                switch (event.key.keysym.sym)
                {
                case SDLK_DOWN:
                    i++;
                    break;
                case SDLK_UP:
                    i=i+2;

                    break;
                case SDLK_ESCAPE:
                    done=1;
                    break;
                case SDLK_RETURN:
                    done=1;
                    break;
                }
                break;
            }

        }

        /*****************************(2)Display everything on screen surface *************************/


        if(i==0)
            // SDL_BlitSurface(b1,NULL,screen,&sb1);
            SDL_BlitSurface(b1,NULL,screen,NULL);
        if(i==1)
            //  SDL_BlitSurface(b1,NULL,screen,&sb2);
            SDL_BlitSurface(b2,NULL,screen,NULL);
        if(i==2)
            //  SDL_BlitSurface(b3,NULL,screen,&sb3);
            SDL_BlitSurface(b3,NULL,screen,NULL);

        /*SDL_BlitSurface(b1,NULL,screen,&sb1);
        SDL_BlitSurface(b2,NULL,screen,&sb2);
        SDL_BlitSurface(b3,NULL,screen,&sb3);*/



        /*****************(3 + 4) Copy screen to graphics memory and clean screen (Flip)*****************/
        SDL_Flip(screen);
        i=i%3;

        if(i==1000)
            i=0;
    } /***end of Menu LOOOP***/

    SDL_EnableKeyRepeat(10,10);

    if(i==0) /***** i=0---->start Game i=1 -----> exit i=2----> display help ****/
    {
        done=0;

        while(!done)
        {
            while(SDL_PollEvent(&event))
            {
                switch(event.type)
                {
                case SDL_QUIT:
                    done=1;
                case SDL_KEYDOWN:
                    switch( event.key.keysym.sym )
                    {
                    case SDLK_LEFT:
                    {
                        dest1.x =dest1.x-5;
                        break;
                    }
                    case SDLK_RIGHT:
                    {
                        if (dest1.x+5<900)
                            dest1.x =dest1.x+5;
                        break;
                    }
                    case SDLK_UP:
                    {
                        dest1.y=dest1.y-5 ;
                        break;
                    }
                    case SDLK_DOWN:
                    {
                        if(dest1.y<518)
                            dest1.y=dest1.y+5 ;
                        break;
                    }
                    // exit if ESCAPE is pressed
                    case SDLK_ESCAPE:
                    {
                        done = 0;
                        break;
                    }
                    default:
                        break;
                    }

                    break;
                }

            }

            SDL_BlitSurface(image,NULL, screen,NULL);
            //Flip the backbuffer to the primary Hardware Video Memory
            SDL_BlitSurface(azerty, &src1, screen, &dest1);


            SDL_Flip(screen);
        }



    }
    if(i==1) //exit from game
        printf("\n****************END**************** ");

    //Release the surface
    fprintf(stdout,"%d\n",SDL_GetTicks());
    SDL_FreeSurface(image);
    SDL_FreeSurface(azerty);
    SDL_FreeSurface(b1);
    SDL_FreeSurface(b2);
    SDL_FreeSurface(b3);
    //Return success!

    return 0;
}

